package com.skyable.gateway.filter;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.constants.param.CommonParams;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author GaoYuan
 * @title: BaseGlobalFilter
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1514:10
 */
public class BaseGlobalFilter {

    static Set<String> exclusionUrlSet = Sets.newConcurrentHashSet();

    static {
        exclusionUrlSet.add("/auth/**");
        exclusionUrlSet.add("/**/public/**");
        exclusionUrlSet.add("/**/v2/api-docs");
    }

    /**
     * 返回错误信息
     * @param response
     * @param code
     * @param value
     * @return
     */
    Mono<Void> error(ServerHttpResponse response, Integer code, String value) {
        //返回错误
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        ResponseResult result = ResponseResult.error(code, value);
        DataBuffer buffer = response.bufferFactory().wrap(JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Mono.just(buffer));
    }

    /**
     * 认证成功
     * @param exchange
     * @param chain
     * @param result
     * @return
     */
    Mono<Void> success(ServerWebExchange exchange, GatewayFilterChain chain, Map<String,Object> result) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder mutate = request.mutate();
        mutate.header(CommonParams.HEADER_USER_NAME, String.valueOf(result.get(CommonParams.HEADER_USER_NAME)));
        mutate.header(CommonParams.HEADER_USER_DOMAIN_ID, String.valueOf(result.get(CommonParams.HEADER_USER_DOMAIN_ID)));
        ServerHttpRequest buildRequest = mutate.build();
        return chain.filter(exchange.mutate()
                .request(buildRequest)
                .build());
    }

    /**
     * 请求是否放行
     * @param path
     * @return
     */
    Boolean isRelease(String path) {
        Boolean result = false;
        for(String url : exclusionUrlSet) {
            if(path.matches(url.replace("*",".*"))) {
                result = true;
                break;
            }
        }
        return result;
    }

    /**
     * 用户是否有请求接口权限
     * @param path
     * @return
     */
    Boolean isAuthorizated(String path, List<String> permissionCodeList) {
        //TODO 判断用户是否有权限
        if(permissionCodeList == null || permissionCodeList.isEmpty()) {
            return false;
        }
        return true;
    }
}
